using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Content;

using QuestFramework;

namespace QuestFramework.UI
{
    public class DialogWindow : Window
    {
        private Dialog dialogEngine;
        private WindowTextureElement buttonImage;
        private int selectedResponse;

        public DialogWindow(Game game) : base(game)
        {
            dialogEngine = new Dialog(game);
            selectedResponse = 0;
        }

        public override void Initialize()
        {
            windowPosition = new Vector2(10, 400);
            windowFont = parentGame.Content.Load<SpriteFont>("Fonts\\Pericles_Small");
            buttonImage.Texture = parentGame.Content.Load<Texture2D>("Textures\\button_a");
            buttonImage.TextureColor = Color.White;
            buttonImage.TexturePosition = new Vector2(windowPosition.X + 10.0f, windowPosition.Y + 45.0f);
            buttonImage.TextureSize = new Vector2(32.0f, 32.0f);
            backgroundTexture = parentGame.Content.Load<Texture2D>("Textures\\dialogwindow");

            base.Initialize();
        }

        public override void Draw(GameTime gameTime)
        {
            if (!dialogEngine.IsConversationOver)
            {
                Sprites.Begin();
                Sprites.DrawString(windowFont, dialogEngine.NpcName + ": " + dialogEngine.RequestString, new Vector2(windowPosition.X + 10.0f, windowPosition.Y + 10.0f), Color.Black, 0.0f, Vector2.Zero, 1.0f, SpriteEffects.None, 0.0f);

                float responseYPos = windowPosition.Y;
                for (int responseChoices = 0; responseChoices < dialogEngine.ResponseStrings.Count; responseChoices++)
                {
                    Sprites.DrawString(windowFont, dialogEngine.ResponseStrings[responseChoices], new Vector2(windowPosition.X + 50.0f, responseYPos + 50.0f), Color.Black, 0.0f, Vector2.Zero, 1.0f, SpriteEffects.None, 0.0f);
                    responseYPos += 30;
                }

                Sprites.Draw(buttonImage.Texture, buttonImage.TextureRectangle, buttonImage.TextureColor);
                Sprites.End();
            }
            else
            {
                visible = false;
            }

            base.Draw(gameTime);
        }

        public void Show(int conversationFileNumber)
        {
            visible = true;
            dialogEngine.StartConversation(conversationFileNumber);
        }

        public void NextResponse()
        {
            if (selectedResponse != dialogEngine.ResponseStrings.Count - 1)
            {
                selectedResponse++;
                buttonImage.TexturePosition = new Vector2(buttonImage.TexturePosition.X, buttonImage.TexturePosition.Y + 30);
            }
        }

        public void PreviousResponse()
        {
            if (selectedResponse != 0)
            {
                selectedResponse--;
                buttonImage.TexturePosition = new Vector2(buttonImage.TexturePosition.X, buttonImage.TexturePosition.Y - 30);
            }
        }

        public void SubmitResponse()
        {
            dialogEngine.SubmitResponse(selectedResponse);
            buttonImage.TexturePosition = new Vector2(windowPosition.X + 10.0f, windowPosition.Y + 45.0f);
            selectedResponse = 0;
        }

    }
}
